package com.admission.web;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.admission.model.Environment;
import com.admission.model.EnvironmentPicture;
import com.admission.service.EnvironmentManager;
import com.admission.service.EnvironmentPictureManager;
import com.admission.util.ArrayUtils;

@Namespace("/base")
@Results({@Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class EnvironmentAction extends CrudActionSupport<Environment> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private EnvironmentManager environmentManager;
	private EnvironmentPictureManager environmentPictureManager;
	private Environment entity;
	private List<EnvironmentPicture> pictures;
	private String type;
	private ArrayUtils arrayUtils;

	public Environment getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		if (StringUtils.isEmpty(type))
			type = "00000007";
		entity = environmentManager.getByType(type);
		if (entity != null)
			pictures = environmentPictureManager.getSubPicture(entity.getId());
		return "list";

	}

	/**
	 * 判断状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkOnlineConsult() throws Exception {
		if (environmentManager.checkConsult().equals("1"))
			return list();
		else
			return "fail";
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		return list();
	}

	@Override
	public String delete() throws Exception {
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new Environment();
		} else {
			entity = environmentManager.getById(id);
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public EnvironmentManager getEnvironmentManager() {
		return environmentManager;
	}

	public void setEnvironmentManager(EnvironmentManager environmentManager) {
		this.environmentManager = environmentManager;
	}

	public EnvironmentPictureManager getEnvironmentPictureManager() {
		return environmentPictureManager;
	}

	public void setEnvironmentPictureManager(
			EnvironmentPictureManager environmentPictureManager) {
		this.environmentPictureManager = environmentPictureManager;
	}

	public Environment getEntity() {
		return entity;
	}

	public void setEntity(Environment entity) {
		this.entity = entity;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<EnvironmentPicture> getPictures() {
		return pictures;
	}

	public void setPictures(List<EnvironmentPicture> pictures) {
		this.pictures = pictures;
	}

	public ArrayUtils getArrayUtils() {
		return arrayUtils;
	}

	@Autowired
	public void setArrayUtils(ArrayUtils arrayUtils) {
		this.arrayUtils = arrayUtils;
	}

}
